

#include "../sc-s/main.h"
#include "../sc-s/lcd.h"
#include "../sc-s/sc-s.h"
#include "../sc-s/utils.h"
#include "../sc-s/trans-s.h"
#include <stdlib.h>

#define HR_ZPB	15		// 1.5%
#define HR_ZNB	25		//-2.5%

#define LR_ZPB	9		// 0.09%
#define LR_ZNB	20		//-2.00%

u16 meas = 0;


#define DEFAULT_AWP_DELAY	45

void awp_check()
{

	if (!Awp)
	{
		if (awp_flags & POWERFLAG)
		{
			ALARM1_OFF;
			ALARM2_OFF;
			return;
		}
		
		if (awp_flags & AL1_FLAG)
		{
			ALARM1_OFF;
		}
		else
		{
			ALARM1_ON;
		}
		
		if (awp_flags & AL2_FLAG)
		{
			ALARM2_OFF;
		}
		else
		{
			ALARM2_ON;
		}
		
		
		return;
	}
	
	if (meas & 0x8000)
	{
		ALARM1_OFF;
		ALARM2_OFF;
		return;
	}
	
	u8 ok1 = 1;
	u8 ok2 = 1;
	

	if (awp_flags & POWERFLAG)
	{
		ok1 = 0;
		ok2 = 0;
	}
	
	if (ok1 && (!(awp_flags & AL1_FLAG)))
	{
		ALARM1_ON;
	}
	else
	{
		ALARM1_OFF;
	}
	
	if (ok2 && (!(awp_flags & AL2_FLAG)))
	{
		ALARM2_ON;
	}
	else
	{
		ALARM2_OFF;
	}

}


void show_rh()
{
	char str[10];
	lcd_home();

	pom_rh &= 0x3FFF;
	
	if (pom_rh > 1050)
	{
		
		lcd_def2(1,cuCharA);
		lcd_def2(2,cuCharL);
		lcd_home();
		lcd_print("B");
		lcd_sign(2);
		lcd_sign(1);
		lcd_print("D RH%!");
		return;
	}
	
	fuse1(str,pom_rh + corr_rh);
	lcd_print(str);
	if (strlen(str) < 4)
		lcd_print(" ");
	lcd_print(" %RH");
	
	// 24.4 %RH
}

void show_pr()
{
	char str[10];


	pom_pr &= 0x3FFF;
	
	if (pom_pr < pr_range_l && (MIN))
	{
		lcd_home();
		lcd_print("<");
		itoa(pr_range_l/10,str,10);
		lcd_print(str);
		lcd_print("hPa ");
		return;
	}
	if (pom_pr > pr_range_h && (MIN))
	{
		lcd_home();
		lcd_print(">");
		itoa(pr_range_h/10,str,10);
		lcd_print(str);
		lcd_print("hPa ");
		return;
	}
	
	if ((pom_pr < 5000 || pom_pr > 14000) && (MIN))
	{
		lcd_def2(2,cuCharA);
		lcd_def2(3,cuCharL);
		lcd_home();
		lcd_print("B");
		lcd_sign(3);
		lcd_sign(2);
		lcd_print("D ");

		lcd_print("hPa ");
		return;
	}
	
	fuse1(str,pom_pr);
	lcd_home();
	lcd_print(str);

	lcd_print("hPa");
	// 1000.0hP	
}


void show_pr_spec()
{
	char str[10];

	pom_pr &= 0x3FFF;


	fuse1(str,pom_pr);

	lcd_print(str);
	lcd_print(" ");

	// 1000.0hP
}

void show_prd()
{
	char str[10];
 
	lcd_home();

	pom_pr &= 0x3FFF;
	
	if (pom_pr == 0x3FFF)
	{
		lcd_print(" < 0 hPa");
		return;
	}
	if (pom_pr == 0x3FFE)
	{
		lcd_print(">MAX hPa");
		return;
	}
	
	fuse1(str,pom_pr);
	lcd_print(str);
	u8 len = strlen(str);
	for (u8 c = 0; c < 5 - len; c++)
	{
		lcd_print(" ");
	}		
	lcd_print("hPa");
	// 1000.0hP
}


void show_prd_spec()
{
	char str[10];


	pom_pr &= 0x3FFF;
	
	if (pom_pr == 0x3FFF)
	{ 
		lcd_print(" < 0 hPa");
		return;
	}
	if (pom_pr == 0x3FFE)
	{
		lcd_print(">MAX hPa");
		return;
	}	
	fuse1(str,pom_pr);
	lcd_print(str);
	lcd_print("hPa   ");
	//10kPa = 100.0hPa
	// 1000.0hP
}

void show_tmp()
{
	char str[10];

	pom_tm &= 0x3FFF;
	i16 pom = pom_tm;
	
	lcd_def(1,7,5,7,0,0,0,0,0);		// *C
	
	
	if (pom_tm > 700)
	{
		
		lcd_def2(2,cuCharA);
		lcd_def2(3,cuCharL); 
		lcd_home();
		lcd_print("B");
		lcd_sign(3);
		lcd_sign(2);
		lcd_print("D "); 
		lcd_sign(1);
		lcd_print("C ");
		return;
	}
		
	lcd_home();
	pom += corr_t;
	
	fuse1i(str,pom - 200);
	lcd_print(" ");
	lcd_print(str);
	if (strlen(str) < 4)
		lcd_print(" ");
	lcd_sign(1);
	lcd_print("C ");
	// " 23.4*C "
}
	




void show_media(u16 value, u8 flags)
{
	lcd_home();

static u8 show_med = 0;
static u8 disp_delay = 0;

#ifdef MEDIA_PS

	if (show_med == 0)
	{
		if (sh_flag & 0x01)
		{
			show_pr();
		}
		else
		{
			show_med ++;
		}			
	}	
	if (show_med == 1)
	{
		if (sh_flag & 0x02)
		{
			show_rh();
		}
		else
		{
			show_med ++;
		}
	}
	if (show_med == 2)
	{
		if (sh_flag & 0x04)
		{
			show_tmp();
		}
		else
		{
			show_med ++;
		}
	}
	if (show_med > 2)
		show_med = 0;
		
	disp_delay ++;
	if (disp_delay > 30)
	{
		disp_delay = 0;
		show_med ++;
		if (show_med > 2)
		{
			show_med = 0;
		}
	}
#elif MEDIA_PD
	show_prd();

#endif
	
}
